#!/bin/bash
# * =====================================================
# * Copyright © hk. 2022-2025. All rights reserved.
# * File name  : 1.sh
# * Author     : 上上签
# * Date       : 2023-04-29
# * ======================================================
##
SCRIPT_NAME=${0#*/}
##======================================================
BLACK="\033[1;30m"
RED='\033[1;31m'    # 红
GREEN='\033[1;32m'  # 绿
YELLOW='\033[1;33m' # 黄
BLUE='\033[1;34m'   # 蓝
PINK='\033[1;35m'   # 紫
CYAN='\033[1;36m'   # 青
WHITE='\033[1;37m'  # 白
CLS='\033[0m'       # 清除颜色

INFO="${GREEN}INFO  ${CLS}"
WARN="${YELLOW}WARN  ${CLS}"
ERR="${RED}ERROR  ${CLS}"

##======================================================
function gulp_local_preview()
{
    echo -e "${INFO}正在清理本地缓存..."
	hexo clean
	echo -e "${INFO}正在编译静态页面..."
	hexo generate

	# 命令不存在，则提示出错
	if ! gulp -v >/dev/null 2>&1 ; then
    	echo -e "${INFO}安装全局 Gulp ..."
		npm install gulp -g # 全局安装 CLI
		sleep 1s
		echo -e "${INFO}安装本地 Gulp ..."
		npm install gulp # 目录内安装 Local
	fi
	gulp # gulp插件，若未配置则无需开启，开启会报错

	echo -e "${INFO}正在启动本地预览,可以按Ctrl+C退出!!!"
	hexo server
}

function local_preview()
{
    echo -e "${INFO}正在清理本地缓存..."
	hexo clean
	echo -e "${INFO}正在编译静态页面..."
	hexo generate
	echo -e "${INFO}正在启动本地预览,可以按Ctrl+C退出!!!"
	hexo server
}

function Initialize_hexo_site()
{
   echo -e "${INFO}正在检测相关软件是否安装..."
	# 命令不存在，则提示出错
	if ! node -v >/dev/null 2>&1 ; then
    	echo -e "${ERROR}node未安装,请安装node后重试..."
		return
	elif ! npm -v >/dev/null 2>&1 ; then
		echo -e "${ERROR}npm未安装,请安装npm后重试..."
		return
	elif ! hexo -v >/dev/null 2>&1 ; then
		echo -e "${ERROR}Hexo未安装..."
		sleep 2s
		echo -e "${INFO}正在安装Hexo..."
		npm install -g hexo-cli
		echo -e "${INFO}安装完成!"
	fi
	echo -e "${INFO}相关软件已准备就绪!!!"
	# 开始初始化站点
	read -p "请输入Hexo站点名称:" HexoFileName
	if [ -d "./${HexoFileName}" ]; then
		echo -e "${WARN}${HexoFileName}已存在!!!"
		sleep 3s
	else
		mkdir -pv ${HexoFileName}
	fi
	cd ${HexoFileName}
	echo -e "${PINK}current path:$(pwd)${CLS}"
	echo -e "${INFO}正在初始化站点..."
	hexo init   # 文件非空的话这里会报错，后边程序会停止
	if [ ${?} != 0 ]; then
		echo -e "${ERROR}初始化失败,目录可能非空！!!"
		return
	fi
	echo -e "${INFO}正在安装必要插件..."
	npm install --save
	echo -e "${INFO}正在安装部署工具..."
	npm install hexo-deployer-git --save
	echo -e "${INFO}请查看您当前的软件版本..."
	echo "node versin:$(node -v)"
	echo "npm versin:$(npm -v)"
	echo "hexo versin:$(hexo -v)"
	cp -i ../${SCRIPT_NAME#*/} .
	echo -e "${INFO}初始化完成,请进入 $(pwd) 重新运行该脚本完成其他配置!!!"
}

function install_NexT_themes()
{
	echo -e "${WARN}提示:需要在Hexo站点根目录下进行该操作!!!"
	if [ -d "themes/next" ];then
		rm -rf themes/next
	fi
	echo -e "${INFO}正在 clone 主题仓库.."
	git clone https://github.com/next-theme/hexo-theme-next themes/next
	echo -e "${INFO}正在删除 themes/next/.git文件..."
	rm -rf themes/next/.git

	echo -e "${INFO}正在修改相关配置文件..."
	sed -i 's/theme: landscape/theme: next/g' ./_config.yml
	rm -f _config.landscape.yml
	cp -pv themes/next/_config.yml _config_next.yml

	echo -e "${INFO}主题安装完成!!!"
}

function deploy_hexo()
{
	echo -e "${INFO}正在清理本地缓存..."
	hexo clean
	# echo -e "${INFO}正在更新番剧列表..."
	# hexo bangumi -u #bilibili追番插件，未配置无需开启
	echo -e "${INFO}正在编译静态页面..."
	hexo generate
	#echo -e "${INFO}正在压缩静态资源..."
	#gulp #gulp插件，若未配置则无需开启，开启会报错
	echo -e "${INFO}正在将最新修改部署至Hexo..."
	hexo deploy
	echo -e "${INFO}部署完成，您的网站已经是最新版本!!!"
	sleep 1s
}

function update_hexo_plugin()
{
	echo -e "${INFO}请先确认当前版本 ..."
	hexo version
	sleep 3s
	echo -e "${INFO}正在升级全局升级hexo-cli..."
	npm install hexo-cli -g
	echo -e "${INFO}hexo-cli升级完成,请查看当前版本。"
	hexo version
	sleep 3s
	echo -e "${INFO}正在升级升级npm-check..."
	npm install -g npm-check
	echo -e "${INFO}npm-check升级完成!"
	echo -e "${INFO}正在使用npm-check检查系统是否有可升级插件..."
	npm-check -u
	sleep 3s
	echo -e "${INFO}正在升级npm-upgrade..."
	npm install -g npm-upgrade
	echo -e "${INFO}正在使用npm-upgrade升级插件..."
	echo -e "${INFO}您可以在接下来的过程中主动选择是否升级插件"
	npm-upgrade
	sleep 1s
	echo -e "${INFO}正在为您保存升级结果..."
	npm update -g
	npm update --save
	echo -e "${INFO}恭喜您,您的Hexo已经是最新版本!!!"
	sleep 1s
}

function replace_npm_source()
{
	echo -e "${INFO}正在查询当前npm源 ..."
	OfficialSource="https://registry.npmjs.org/"
	Current=`npm config get registry`
	echo -e "${INFO}当前源为:${Current}"
	read -p "是否确认切换(y or n)" ret
	if [ "${ret}" = "y" ] || [ "${ret}" = "" ]; then
		echo -e "${INFO}正在切换npm源..."
		if [ "${Current}" = "${OfficialSource}" ]; then
			npm config set registry https://registry.npmmirror.com
		else
			npm config set registry https://registry.npmjs.org
		fi
		Current=`npm config get registry`
		echo -e "${INFO}切换后npm源为:${Current}"
		sleep 2s
	fi
}

function ssh_key_config()
{
	echo -e "${INFO}正在检测相关软件是否安装..."
	# 命令不存在，则提示出错
	if ! git --version >/dev/null 2>&1 ; then
    	echo -e "${ERROR}Git未安装,请安装Git后重试..."
		return
	fi
	echo -e "${INFO}Git版本为: $(git --version)"
	sleep 2s
	echo -e "${INFO}正在检测是否可以连接到 GitHub..."
	ssh -T git@github.com
	read -p "输出是否含有: git@github.com: Permission denied (publickey). ?(y or n)" res
	if [ "${res}" = "y" ] || [ "${res}" = "" ]; then
		echo -e "${ERROR}ssh key未安装,无法连接到 GitHub"
	else
		echo -e "${INFO}ssh key已安装,可正常使用"
		read -p "是否重新安装 ssh key ?(y or n)" res
		if [ "${res}" = "y" ] || [ "${res}" = "" ]; then
			rm -rf ~/.ssh
		else
			return
		fi
	fi
	echo -e "${INFO}开始安装 SSH !"
	read -p "请输入 GitHub 用户名: " githubuser
	read -p "请输入 GitHub 邮箱: " githubemail
	git config --global user.name "${githubuser}"
	git config --global user.email "${githubemail}"
	ssh-keygen -t ed25519 -C ${githubemail}
	echo -e "${INFO}即将打开sshkey,复制后可按 q 返回..."
	sleep 2ss
	less ~/.ssh/id_ed25519.pub
	echo -e "${INFO}配置完成,请将sshkey添加到GitHub!!!"
	echo -e "${INFO}【Github】→【登录账号】→【头像】→【Setting】→【SSH and GPG keys 】→【SSH keys】→【New SSH key】"
	echo -e "${INFO}https://github.com/settings/keys"
    sleep 2s
}

function echo_menu()
{
	echo "================================================="
	echo -e "${GREEN}               Ubuntu Config ${CLS}"
	echo -e "${GREEN}                by @上上签    ${CLS}"
	echo "================================================="
	echo -e "${PINK}current path:$(pwd)${CLS}"
    echo ""
    echo -e "${GREEN}*[0]${CLS} 开启本地预览"
    echo -e "${GREEN}*[1]${CLS} 初始化Hexo站点"
    echo -e "${GREEN}*[2]${CLS} 升级NexT主题"
    echo -e "${GREEN}*[3]${CLS} GULP 后开启本地预览"
    echo -e "${GREEN}*[4]${CLS} 部署页面到博客网站"
    echo -e "${GREEN}*[5]${CLS} 升级 Hexo 及插件(慎用)"
    echo -e "${GREEN}*[6]${CLS} npm 源切换"
    echo -e "${GREEN}*[7]${CLS} SSH 配置"
    echo -e "${PINK}【注意】使用之前还请确保已安装 Git和Node.${CLS}"
	echo "================================================="
}

function func_process()
{
	read -p "请选择功能,默认选择0:" choose
	case "${choose}" in
		"0") local_preview;;
		"1") Initialize_hexo_site;;
		"2") install_NexT_themes;;
		"3") gulp_local_preview;;
		"4") deploy_hexo;;
		"5") update_hexo_plugin;;
		"6") replace_npm_source;;
		"7") ssh_key_config;;
		*) local_preview;;
	esac
}
##======================================================
echo_menu
func_process